<template>
	<el-dialog title="请使用微信扫码付款" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<!-- <sc-qr-code v-if="url" :text="url" logo="img/logo.png"></sc-qr-code> -->
		<div class="code"><sc-qr-code v-if="url" size="300" :text="url"></sc-qr-code></div>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" @click="submit()">已 付 款</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				visible: false,
				//表单数据
				form: {
					id:"",
				},
				url: '',
				timer: null
			}
		},
		mounted() {

		},
		methods: {
			async getPrepay(ordernum) {
				var reqData = {ordernum: ordernum}
				const loading = this.$loading();
				var res = await this.$API.order.express.prepay.get(reqData);
				loading.close();
				this.visible = true;
				if(res.code == 200){
					this.url = res.data.code_url
					this.setTime(res.data.out_trade_no)
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			async setTime(out_trade_no) {
				this.timer = setInterval(async () => {
					if (this.istimer) return
					this.istimer = true
					let reqData = {out_trade_no: out_trade_no}
					let res = await this.$API.order.express.unionpayCheck.get(reqData);
					this.istimer = false
					if(res.code == 200){
						if (res.data.status == 0) return
						if (res.data.status == 1) {
							this.$alert(res.msg, "提示", {type: 'success'})
							clearInterval(this.timer)
							this.timer = null
							setTimeout(() => {
								this.visible = false;
							}, 800)
						} else {
							this.$alert(res.msg, "提示", {type: 'error'})
							clearInterval(this.timer)
							this.timer = null
							this.getPrepay()
						}
					}else{
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				}, 2000)
			},
			//表单提交方法
			submit(){
				this.$emit('success', {}, '')
				this.visible = false;
			},
			//显示
			open(){
				// this.visible = true;
				return this
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.getPrepay(data.ordernum)
				// this.form.amount = data.amount
				// this.form.status = data.status
				// this.form.remark = data.remark

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		},
		beforeUnmount() {
			if (this.timer) clearInterval(this.timer); this.timer = null
		}
	}
</script>

<style lang="scss" scoped>
	.code{
		width: 100%;
		display: flex;
		justify-content: center;
	}
</style>
